package
{
	import Dialogs.D1;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class StatAssigner
	{
		private var baseDialog:D1;
		public var m_display:Sprite = new Sprite();
		private var m_store:Store;
		
		private var pointsLeftTF:TextField = new TextField();
		private var qualityTF:TextField = new TextField();
		private var stockTF:TextField = new TextField();

		public function StatAssigner(store:Store)
		{
			baseDialog = new D1('Assign Points For ' + store.getShopName(), 400, 200);
			m_display.addChild(baseDialog.display);
			
			m_store = store;
			var qualityPlus:Sprite = Util.getButton(' + ',0x228822);
			var stockPlus:Sprite = Util.getButton(' + ',0x228822);

			Util.applyStyleH3(qualityTF);
			Util.applyStyleH3(stockTF);
			Util.applyStyleH3(pointsLeftTF, 0xffff00);
			pointsLeftTF.text = 'Points Left: ';
			
			var qualityContainer:Sprite = new Sprite();
			var stockContainer:Sprite = new Sprite();
			
			qualityContainer.addChild(qualityPlus);
			qualityContainer.addChild(qualityTF);
			qualityTF.x = stockPlus.width + Game.PADDING;
			
			stockContainer.addChild(stockPlus);
			stockContainer.addChild(stockTF);
			stockTF.x = stockPlus.width + Game.PADDING;

			
			m_display.addChild(qualityContainer);
			qualityContainer.y = baseDialog.title_tf.height + Game.PADDING*3;
			qualityContainer.x = Game.PADDING;
			
			m_display.addChild(stockContainer);
			stockContainer.y = qualityContainer.y + qualityContainer.height + Game.PADDING;
			stockContainer.x = Game.PADDING;
			
			m_display.addChild(pointsLeftTF);
			pointsLeftTF.y = m_display.height - pointsLeftTF.height - Game.PADDING*2;
			pointsLeftTF.x = m_display.width/2 - pointsLeftTF.width/2;
			
			stockPlus.addEventListener(MouseEvent.CLICK, function():void{
				if(store.statPoints > 0){
					store.maxStock++;
					store.statPoints--;
					update();
				}
			});
			
			qualityPlus.addEventListener(MouseEvent.CLICK, function():void{
				if(store.statPoints > 0){
					store.quality++;
					store.statPoints--;
					update();
				}
			});			
			
			update();
		}
		
		
		public function get display():DisplayObject{
			return m_display;
		}
		
		public function set onCloseCallback(closeCallback:Function):void{
			baseDialog.closeClickCallback = closeCallback;
		}
		
		private function update():void{
			qualityTF.text = 'Quality  ' + m_store.quality;
			qualityTF.autoSize = TextFieldAutoSize.LEFT;
			stockTF.text = 'Max Stock  ' + m_store.maxStock;
			stockTF.autoSize = TextFieldAutoSize.LEFT;	
			
			pointsLeftTF.text = 'Points Left: ' + m_store.statPoints;
		}
		
	}
}